package com.sky.controller;

/*
 *用户：sky-吴
 *日期：2020/4/23
 */

import com.jfinal.core.Controller;
import io.jboot.web.controller.annotation.RequestMapping;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import redis.clients.jedis.Connection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


/**
 * Web Service 服务引用地址为：
 *
 */
@RequestMapping("/msg")
public class MessageController extends Controller {


	/*@吴世凯 短信通道那个我们先对接 2.2发送短信 接口看看效果。
需要的账号密码之类的要素，问下@苏海媚*/
	public void sendIndividualMsg() {


	}

	/**
	 * http方式发送短信平台的url :http://118.178.116.15/winnerrxd/api/mass/SendMsg
	 * 平台URL是:http://118.178.116.15/winnerrxd/api/mass/  其中指令是 SendMsg
	 * userCode:STJ_XTY_00001001
	 * userPass:MXHunOsi
	 * localhost:8080/msg/sendMsg
	 */
	public void sendMsg() {

		///String url = "http://118.178.116.15/winnerrxd/api/trigger/SendMsg";
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		String url = "http://118.178.116.15:8443/winnerrxd/api/trigger/SendMsg ";
		String userCode="STJ_XTY_00001001";
		String userPass="MXHunOsi";
		String DesNo="15932072636";
		String Msg="【通联支付】1234";
		Integer smsType=102;
		nvps.add(new BasicNameValuePair("userCode", userCode));
		nvps.add(new BasicNameValuePair("userPass", userPass));
		nvps.add(new BasicNameValuePair("DesNo", DesNo));
		nvps.add(new BasicNameValuePair("Msg", Msg));
		//nvps.add(new BasicNameValuePair("smsType", smsType.toString()));
		String post = httpPost(url, nvps); //post 请求
		System.out.println(post);
		String getparam = "userCode=URLEncoder.encode("+userCode+")&userPass=URLEncoder.encode("+userPass+") & DesNo = " +
				"URLEncoder" +
				".encode" +
				"("+DesNo+") & Msg = URLEncoder.encode("+Msg+") & smsType = URLEncoder.encode("+smsType+")";
		String result = httpGet(url, getparam); //get 请求
		System.out.println("raw resultString is ===>"+result);


	}

	public static String httpPost(String url, List<NameValuePair> params) {
		String result = "";
		try {
			HttpClient httpclient = new DefaultHttpClient();
			//httpclient= (HttpClient) HttpClientBuilder.create();
			HttpPost httpPost = new HttpPost(url);
			//在这里解码返回来的数据
			httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
			HttpResponse response = httpclient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream instreams = entity.getContent();
				result = convertStreamToString(instreams);
				System.out.println("decoded String is===> "+result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String httpGet(String url, String params) {
		String result = "";
		try {
			HttpClient client = new DefaultHttpClient();
			if (params != "") {
				url = url + "?" + params;
			}
			HttpGet httpget = new HttpGet(url);
			HttpResponse response = client.execute(httpget);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream instreams = entity.getContent();
				result = convertStreamToString(instreams);
				System.out.println(result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));

		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}


}
